package com.nation.nationslidemenu;



import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


import com.nation.nationslidemenu.Constants.Extra;
import com.nostra13.universalimageloader.utils.L;
import com.slidinglayer.SlidingLayer;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;



public class MainActivity extends Activity implements OnClickListener {

	private SlidingLayer mSlidingLayer;
	

	private String mStickContainerToRightLeftOrMiddle = "left";
	private boolean mShowShadow;
	private Button button1;

	
	//===================Layout Menu======================
	private FrameLayout lay_my_money,lay_Morningstar,lay_Top40,lay_Video,lay_Photo;
	
	private RelativeLayout lay_main;
	
	
	private static final String TEST_FILE_NAME = "Universal Image Loader @#&=+-_.,!()~'%20.png";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		button1 = (Button) findViewById(R.id.button1);
		button1.setOnClickListener(this);

		
		//===================Layout SlideMenu======================
		lay_my_money=(FrameLayout)findViewById(R.id.lay_my_money);
		lay_Morningstar=(FrameLayout)findViewById(R.id.lay_Morningstar);
		lay_Top40=(FrameLayout)findViewById(R.id.lay_Top40);
		lay_Video=(FrameLayout)findViewById(R.id.lay_Video);
		lay_Photo=(FrameLayout)findViewById(R.id.lay_Photo);
		
		
		
		//============================InitSlideMenu=================
		getPrefs();
		bindViews();
		initState();
		
		
		lay_main=(RelativeLayout)findViewById(R.id.lay_main);
		lay_main.setOnClickListener(this);

		

		File testImageOnSdCard = new File("/mnt/sdcard", TEST_FILE_NAME);
		if (!testImageOnSdCard.exists()) {
			copyTestImageToSdCard(testImageOnSdCard);
		}
	}

	private void copyTestImageToSdCard(final File testImageOnSdCard) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					InputStream is = getAssets().open(TEST_FILE_NAME);
					FileOutputStream fos = new FileOutputStream(testImageOnSdCard);
					byte[] buffer = new byte[8192];
					int read;
					try {
						while ((read = is.read(buffer)) != -1) {
							fos.write(buffer, 0, read);
						}
					} finally {
						fos.flush();
						fos.close();
						is.close();
					}
				} catch (IOException e) {
					L.w("Can't copy test image onto SD card");
				}
			}
		}).start();
	}
	/**
	 * View binding
	 */
	private void bindViews() {
		mSlidingLayer = (SlidingLayer) findViewById(R.id.slidingLayer1);
	
	}

	/**
	 * Get current value for preferences
	 */
	private void getPrefs() {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);
		mStickContainerToRightLeftOrMiddle = prefs.getString("layer_location",
				"left");
		mShowShadow = prefs.getBoolean("layer_has_shadow", false);
	}

	/**
	 * Initializes the origin state of the layer
	 */
	private void initState() {

		LayoutParams rlp = (LayoutParams) mSlidingLayer.getLayoutParams();
		
		mSlidingLayer.setLayoutParams(rlp);

		// Sets the shadow of the container
		if (mShowShadow) {
			mSlidingLayer.setShadowWidthRes(R.dimen.shadow_width);
			mSlidingLayer.setShadowDrawable(R.drawable.sidebar_shadow);
		} else {
			mSlidingLayer.setShadowWidth(0);
			mSlidingLayer.setShadowDrawable(null);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.button1) {
			if (!mSlidingLayer.isOpened()) {
				mSlidingLayer.openLayer(true);
			}

		}
		
		if(v.getId()==R.id.lay_main)
		{
		if (mSlidingLayer.isOpened()) 
			mSlidingLayer.closeLayer(true);
			
			
		}

	}
	
	public void OnclickSlideMenu(View v)
	{
		Log.e("TNT", "==OnclickSlideMenu==");
		if(v.getId() ==R.id.lay_my_money)
		{
			WebviewActivity.URL="http://thenation.thailistedcompany.com/?device=ipad";
			startActivityForResult(new Intent(this,WebviewActivity.class), 0);			
		}
		else if(v.getId() ==R.id.lay_Morningstar)
		{
			
			WebviewActivity.URL="http://www.nationmultimedia.com/home/morningstar/index1.html";
			startActivityForResult(new Intent(this,WebviewActivity.class), 0);		
		}
		else if(v.getId() ==R.id.lay_Top40)
		{
			WebviewActivity.URL="http://www.nationmultimedia.com/top40/home/demo_ipad/";
			startActivityForResult(new Intent(this,WebviewActivity.class), 0);		
		}
		else if(v.getId() ==R.id.lay_Video)
		{
			Intent intent = new Intent(this,VdoActivity.class);
			intent.putExtra(Extra.IMAGES, Constants.IMAGES);
			startActivity(intent);
			
		}if(v.getId() ==R.id.lay_Photo)
		{
			Intent intent = new Intent(this,PhotoActivity.class);
			intent.putExtra(Extra.IMAGES, Constants.IMAGES);
			startActivity(intent);
			
		}
		
		
		
	}
	
	
	

}
